package com.sls.sys.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.ServerDisponseException;
import com.sls.page.Pagination;
import com.sls.sys.constant.ExceptionCodeDescription;
import com.sls.sys.dao.PermissionDAO;
import com.sls.sys.pojo.PermissionInfo;
import com.sls.sys.pojo.PermissionInfoVO;
import com.sls.sys.querybean.PermissionQuery;
import com.sls.sys.service.PermissionService;

/**
 * 
 * @author wendb
 * 
 */
@Service("permissionService")
public class PermissionServiceImpl implements PermissionService {

	@Resource(name = "permissionDAO")
	private PermissionDAO permissionDAO;
	private Gson gson = null;

	public PermissionServiceImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public void save(String body, Head head) throws Exception {
		PermissionInfoVO PermissionInfo = gson.fromJson(body,
				PermissionInfoVO.class);
		PermissionInfo.setCode(CommonUtil.createRandomNumber());
		if (PermissionInfo == null || PermissionInfo.getName() == null) {
			throw new ServerDisponseException(
					ExceptionCodeDescription.PARSE_REQUEST_ERROR.getCode(),
					ExceptionCodeDescription.PARSE_REQUEST_ERROR
							.getDescription());
		}
		for (int i = 0; i < 20; i++) {
			PermissionInfo pDB = permissionDAO
					.queryInfoByServiceCode(PermissionInfo.getCode());
			if (pDB != null) {
				PermissionInfo.setCode(CommonUtil.createRandomNumber());
			} else {
				break;
			}
		}
		if (PermissionInfo.getOpenNewWindow() == null)
			PermissionInfo.setOpenNewWindow(false);
		PermissionInfo.setUpdateAccount(head.getUserId());
		PermissionInfo.setUpdateDate(new Date());
		this.permissionDAO.insertPermission(PermissionInfo);

	}

	/**
	 * 校验json字符串生成的对象是否为空，若为空，则说明请求的报文格式不正确
	 * 
	 * @param obj
	 * @throws ServerDisponseException
	 */
	private void validate(Object obj) throws ServerDisponseException {
		if (obj == null) {
			throw new ServerDisponseException(
					ExceptionCodeDescription.PARSE_REQUEST_ERROR.getCode(),
					ExceptionCodeDescription.PARSE_REQUEST_ERROR
							.getDescription());
		}
	}

	@Override
	public void update(String body, Head head) throws Exception {
		PermissionInfoVO PermissionInfo = gson.fromJson(body,
				PermissionInfoVO.class);
		this.permissionDAO.updatePermission(PermissionInfo);
	}

	@Override
	public List<PermissionInfoVO> queryAllInfo(String body, Head head)
			throws Exception {
		List<PermissionInfoVO> perList = permissionDAO.queryAll();
		return perList;
	}

	@Override
	public PermissionInfo queryInfoByServiceCode(String body, Head head)
			throws Exception {
		PermissionInfoVO PermissionInfo = gson.fromJson(body,
				PermissionInfoVO.class);
		if (PermissionInfo == null) {
			throw new ServerDisponseException(
					ExceptionCodeDescription.PARSE_REQUEST_ERROR.getCode(),
					ExceptionCodeDescription.PARSE_REQUEST_ERROR
							.getDescription());
		}
		String serviceCode = PermissionInfo.getCode();
		PermissionInfo per = permissionDAO.queryInfoByServiceCode(serviceCode);
		return per;
	}

	@Override
	public Pagination<PermissionInfoVO> queryPermission(String body, Head head)
			throws Exception {
		PermissionQuery permissionQuery = gson.fromJson(body,
				PermissionQuery.class);
		validate(permissionQuery);
		Pagination<PermissionInfoVO> pqInfo = permissionDAO
				.queryPermission(permissionQuery);
		return pqInfo;
	}

	@Override
	public void delete(String body, Head head) throws Exception {
		PermissionInfoVO PermissionInfo = gson.fromJson(body,
				PermissionInfoVO.class);
		String serviceCode = PermissionInfo.getCode();
		permissionDAO.delete(serviceCode);
	}

}
